package com.srs.action.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.srs.action.BaseAction;
import com.srs.dao.SystemParameterDao;
import com.srs.model.SystemParameter;
import com.srs.model.SystemParameterConfig;

/**
 * 
 * @author chienvh
 * 
 */
public class ConfigAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	public static final List<String> PARAMERTER_NAMES = Arrays
			.asList("P_SESSION_LOGGING_KEEP_IN_DAYS", "P_RX_GOOD_THRESHOLD",
					"P_RX_QUALITY_GOOD_THRESHOLD", "P_NUMBER_OF_CALLS",
					"P_NUMBER_OF_AGENTS_LISTED", "P_AGENT_NO_ACTIVITY_IN_DAYS",
					"P_AGENT_NO_CALL_IN_DAYS",
					"P_AGENT_USING_WEAK_RX_IN_SAMPLES",
					"P_AGENT_USING_WEAK_RX_IN_DAYS",
					"P_AGENT_USING_WEAK_RX_QUALITY_IN_SAMPLES",
					"P_AGENT_USING_WEAK_RX_QUALITY_IN_DAYS",
					"P_CSR_GOOD_THRESHOLD_IN_PERCENT",
					"P_SERVICE_CONTROL_KEEP_IN_DAYS");
	public static final List<SystemParameter> PARAMETERS = Arrays
			.asList(
					new SystemParameter("P_SESSION_LOGGING_KEEP_IN_DAYS", "180"),
					new SystemParameter("P_RX_GOOD_THRESHOLD", "-80"),
					new SystemParameter("P_RX_QUALITY_GOOD_THRESHOLD", "4"),
					new SystemParameter("P_NUMBER_OF_CALLS", "5"),
					new SystemParameter("P_NUMBER_OF_AGENTS_LISTED", "100"),
					new SystemParameter("P_AGENT_NO_ACTIVITY_IN_DAYS", "5"),
					new SystemParameter("P_AGENT_NO_CALL_IN_DAYS", "7"),
					new SystemParameter("P_AGENT_USING_WEAK_RX_IN_SAMPLES", "1"),
					new SystemParameter("P_AGENT_USING_WEAK_RX_IN_DAYS", "1"),
					new SystemParameter(
							"P_AGENT_USING_WEAK_RX_QUALITY_IN_SAMPLES", "1"),
					new SystemParameter(
							"P_AGENT_USING_WEAK_RX_QUALITY_IN_DAYS", "1"),
					new SystemParameter("P_CSR_GOOD_THRESHOLD_IN_PERCENT", "90"),
					new SystemParameter("P_SERVICE_CONTROL_KEEP_IN_DAYS", "180"));

	private SystemParameterConfig config;
	private String isReset;
	
	public String edit() {
		// Init Dao 
		System.out.println("ConfigAction::index");
		SystemParameterDao systemParameterDao = new SystemParameterDao();
		List<SystemParameter> systemParameters = systemParameterDao.list();
		
		// Set output to view
		systemParameters = systemParameterDao.list();
		config = new SystemParameterConfig();
		for (SystemParameter systemParameter : systemParameters) {
			config.setValue(systemParameter);
		}

		// Map<String, long>
		// initParams();
		return SUCCESS;
	}

	public String editComplete(){
		// Init Dao 
		System.out.println("ConfigAction::index");
		SystemParameterDao systemParameterDao = new SystemParameterDao();
		List<SystemParameter> systemParameters = systemParameterDao.list();

		if (isReset!=null){
			System.out.println("ConfigAction::editComplete::reset value");
			for (SystemParameter systemParameter : PARAMETERS) {
				config.setValue(systemParameter);
			}
			for (SystemParameter systemParameter : systemParameters) {
				systemParameter.setValue(config);
				systemParameterDao.update(systemParameter);
			}

		} else {
			System.out.println("ConfigAction::editComplete::update value");
			for (SystemParameter systemParameter : systemParameters) {
				systemParameter.setValue(config);
				systemParameterDao.update(systemParameter);
			}
			
		}
		
		// Set output to view
		systemParameters = systemParameterDao.list();
		config = new SystemParameterConfig();
		for (SystemParameter systemParameter : systemParameters) {
			config.setValue(systemParameter);
		}

		// Map<String, long>
		// initParams();
		return SUCCESS;		
	}
	
	private void initParams() {
		System.out.println("ConfigAction::initParams");
		// Get list params
		SystemParameterDao dao = new SystemParameterDao();
		List<SystemParameter> systemParameters = dao.list();

		// List params in list
		List<String> params = new ArrayList<String>();
		for (SystemParameter systemParameter : systemParameters) {
			params.add(systemParameter.getSystemParameterName());
		}

		SystemParameterConfig config = new SystemParameterConfig();

		for (SystemParameter systemParameter : ConfigAction.PARAMETERS) {
			System.out.println(systemParameter.getSystemParameterName());

			if (params.contains(systemParameter.getSystemParameterName())) {
				// config.set
			} else {
				System.out.println("Inser one param");
				System.out.println(systemParameter);
				dao.insert(systemParameter);
			}
		}
	}

	/**
	 * @return the config
	 */
	public SystemParameterConfig getConfig() {
		return config;
	}

	/**
	 * @param config the config to set
	 */
	public void setConfig(SystemParameterConfig config) {
		this.config = config;
	}

	/**
	 * @return the isReset
	 */
	public String getIsReset() {
		return isReset;
	}

	/**
	 * @param isReset the isReset to set
	 */
	public void setIsReset(String isReset) {
		this.isReset = isReset;
	}

	
}
